global working = "$base/replication/data/working/"
global workingnew = "$BS_fold/local1/inputs/"
global tempfold = "$BS_fold/local1/" 
global BSfoldnew = "$BS_fold/local1/final/"

local dataset = "$dataset" 
local samplenametext = "$samplenametext"
local samplenamefile = "$samplenamefile"
global sdv	= 1.645


	use "$BSfoldnew/`dataset'.dta", clear

	preserve
		drop *_F_* *_N2_* *_N3_*
		keep if bs == -1
		unab varl:  *b2* *b3*  
		foreach var of local varl {
			rename `var' se`var'
		}
		save "$working/temp/bstemp2", replace
	restore
	keep if bs == 0
	merge 1:1 bs using "$working/temp/bstemp2"
	drop _merge 
	keep  *b2* *b3*  
	drop *_F_*
	unab varl: *b2* *b3*   
	local mainvars: subinstr local varl "se" "", all
	local mainvars_uniq : list uniq mainvars
	foreach var of local mainvars_uniq {
		replace `var' = `var'[_n-1] if missing(`var')
	}

	gen id = _n
	ren (*_y *_yn *_ycn) (y* yn* ycn*)
	reshape long y yn ycn yse ynse ycnse, i(id) j(variables) string
	foreach var in y yn ycn {
		gen lows_`var' = `var' - ($sdv * `var'se)
		gen highs_`var' = `var' + ($sdv * `var'se)
	}

	
	forval i= 2/3 {
		preserve
			keep if inlist(variables, "adding_term_b`i'", "adding_term_h_j_b`i'", "adding_term_a_j_b`i'", "adding_term_d_j_b`i'")
			replace variables = "Base" if variables == "adding_term_b`i'"
			gen ordering = 1 if variables == "Base"
			replace variables = "MP" if variables == "adding_term_h_j_b`i'"
			replace ordering = 2 if variables == "MP"
			replace variables = "TFPQ" if variables == "adding_term_a_j_b`i'"
			replace ordering = 3 if variables == "TFPQ"
			replace variables = "Demand" if variables == "adding_term_d_j_b`i'"
			replace ordering = 4 if variables == "Demand"
			sencode variables, gen(vars) gsort(ordering)
			gen vars1 = vars+.15
			gen vars2 = vars+.30
			twoway	(scatter y vars, mcolor(blue) msymbol(O)) ///
					(rcap lows_y highs_y vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
					(rcap lows_y highs_y vars if id == 1, lwidth(thin) color(gs6) ) ///
					(scatter yn vars1, mcolor(blue) msymbol(square)) ///
					(rcap lows_yn highs_yn vars1 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
					(rcap lows_yn highs_yn vars1 if id == 1, lwidth(thin) color(gs6) ) ///
					(scatter ycn vars2, mcolor(blue) msymbol(triangle)) ///
					(rcap lows_ycn highs_ycn vars2 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
					(rcap lows_ycn highs_ycn vars2 if id == 1, lwidth(thin) color(blue) ) ///
					, xlabel(1(1)4, angle(45) valuelabel) ///
					ylabel(,grid) ///
					legend(order(1 "Year FE" 4 "Industry and Year FE" 7 "Industry-Year FE") size(medlarge) rows(1) region(lcolor(white))) scheme(s1color) 
			graph export "$figures/`samplenamefile'_b`i'_add.png", as(png) replace
		restore
	}
	
	forval i= 2/3 {
		preserve
			keep if inlist(variables, "drop_term_b`i'", "drop_term_h_j_b`i'", "drop_term_a_j_b`i'", "drop_term_d_j_b`i'")
			replace variables = "Base" if variables == "drop_term_b`i'"
			gen ordering = 1 if variables == "Base"
			replace variables = "MP" if variables == "drop_term_h_j_b`i'"
			replace ordering = 2 if variables == "MP"
			replace variables = "TFPQ" if variables == "drop_term_a_j_b`i'"
			replace ordering = 3 if variables == "TFPQ"
			replace variables = "Demand" if variables == "drop_term_d_j_b`i'"
			replace ordering = 4 if variables == "Demand"
			sencode variables, gen(vars) gsort(ordering)
			gen vars1 = vars+.15
			gen vars2 = vars+.30
			twoway	(scatter y vars, mcolor(blue) msymbol(O)) ///
					(rcap lows_y highs_y vars if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
					(rcap lows_y highs_y vars if id == 1, lwidth(thin) color(blue) ) ///
					(scatter yn vars1, mcolor(blue) msymbol(square)) ///
					(rcap lows_yn highs_yn vars1 if id == 2, lwidth(medthick) lcolor(blue) color(%50)) ///
					(rcap lows_yn highs_yn vars1 if id == 1, lwidth(thin) color(blue) ) ///
					(scatter ycn vars2, mcolor(blue) msymbol(triangle)) ///
					(rcap lows_ycn highs_ycn vars2 if id == 2, lwidth(medthick)  lcolor(blue) color(%50)) ///
					(rcap lows_ycn highs_ycn vars2 if id == 1, lwidth(thin) color(blue) ) ///
					, xlabel(1(1)4, angle(45) valuelabel) ///
					ylabel(,grid) ///
					legend(order(1 "Year FE" 4 "Industry and Year FE" 7 "Industry-Year FE") size(medlarge) rows(1) region(lcolor(white))) scheme(s1color) 
			graph export "$figures/`samplenamefile'_b`i'_drop.png", as(png) replace
		restore
	}
	
	forval i= 2/2 {
		preserve
			keep if inlist(variables, "adding_term_I_b`i'", "adding_term_h_j_I_b`i'", "adding_term_a_j_I_b`i'", "adding_term_d_j_I_b`i'")
			replace variables = "Base" if variables == "adding_term_I_b`i'"
			gen ordering = 1 if variables == "Base"
			replace variables = "MP" if variables == "adding_term_h_j_I_b`i'"
			replace ordering = 2 if variables == "MP"
			replace variables = "TFPQ" if variables == "adding_term_a_j_I_b`i'"
			replace ordering = 3 if variables == "TFPQ"
			replace variables = "Demand" if variables == "adding_term_d_j_I_b`i'"
			replace ordering = 4 if variables == "Demand"
			sencode variables, gen(vars) gsort(ordering)
			gen vars1 = vars+.15
			gen vars2 = vars+.30
			
			twoway	(scatter y vars, mcolor(red) msymbol(square)) ///
					(rcap lows_y highs_y vars if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
					(rcap lows_y highs_y vars if id == 1, lwidth(thin) color(red) ) ///	
					(scatter yn vars1, mcolor(red) msymbol(triangle)) ///
					(rcap lows_yn highs_yn vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
					(rcap lows_yn highs_yn vars1 if id == 1, lwidth(thin) color(red) ) ///
					, xlabel(1(1)4, angle(35) valuelabel) ///
					ylabel(,grid) xtitle("") ///
					legend(order(1 "Year FE" 4 "Industry and Year FE" ) size(medlarge) rows(1) region(lcolor(white))) scheme(s1color) 
				graph export "$figures/`samplenamefile'_add_I_b`i'.png", as(png) replace
		restore
		
		preserve
			keep if inlist(variables, "drop_term_I_b`i'", "drop_term_h_j_I_b`i'", "drop_term_a_j_I_b`i'", "drop_term_d_j_I_b`i'")
			replace variables = "Base" if variables == "drop_term_I_b`i'"
			gen ordering = 1 if variables == "Base"
			replace variables = "MP" if variables == "drop_term_h_j_I_b`i'"
			replace ordering = 2 if variables == "MP"
			replace variables = "TFPQ" if variables == "drop_term_a_j_I_b`i'"
			replace ordering = 3 if variables == "TFPQ"
			replace variables = "Demand" if variables == "drop_term_d_j_I_b`i'"
			replace ordering = 4 if variables == "Demand"
			sencode variables, gen(vars) gsort(ordering)
			gen vars1 = vars+.15
			gen vars2 = vars+.30
			
			twoway	(scatter y vars, mcolor(red) msymbol(square)) ///
					(rcap lows_y highs_y vars if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
					(rcap lows_y highs_y vars if id == 1, lwidth(thin) color(red) ) ///	
					(scatter yn vars1, mcolor(red) msymbol(triangle)) ///
					(rcap lows_yn highs_yn vars1 if id == 2, lwidth(medthick) lcolor(red) color(%50)) ///
					(rcap lows_yn highs_yn vars1 if id == 1, lwidth(thin) color(red) ) ///
					, xlabel(1(1)4, angle(35) valuelabel) ///
					ylabel(,grid) xtitle("") ///
					legend(order(1 "Year FE" 4 "Industry and Year FE" ) size(medlarge) rows(1) region(lcolor(white))) scheme(s1color) 
				graph export "$figures/`samplenamefile'_drop_I_b`i'.png", as(png) replace
		restore
		
	}

